package io.gitee.macxiang.entity;

import java.util.List;

import lombok.Data;

@Data
public class Rule {
  /** 是否或关系 "and" | "or" | 1 | 0 | null | boolean */
  public boolean or = false;
  /** 字段(被比较值) */
  public String field = "";
  /** 比较关系 */
  public String logic = "";
  /** 比较值(由于ts自身问题,当逻辑类型为having时, 数组第二个数据的数据类型只能通过显示定义才能约束) */
  public String value = "";
  public boolean childrenOr = false;
  /** 子逻辑关系 */
  public List<Rule> children;

  public boolean setOr(Object p) {
    String s = p == null ? "" : String.valueOf(p).trim().toLowerCase();
    if (s.equals("or")
        || (!s.isEmpty() && !s.equals("0") && !s.equals("false"))
        || s.equals("true")
        || p instanceof Boolean && ((Boolean) p)) {
      this.or = true;
    } else {
      this.or = false;
    }
    return this.or;
  }

  public boolean setChildrenOr(Object p) {
    String s = p == null ? "" : String.valueOf(p).trim().toLowerCase();
    if (s.equals("childrenor")
        || (!s.isEmpty() && !s.equals("0") && !s.equals("false"))
        || s.equals("true")
        || p instanceof Boolean && ((Boolean) p)) {
      this.childrenOr = true;
    } else {
      this.childrenOr = false;
    }
    return this.childrenOr;
  }

  public Rule() {
  }

  public Rule(boolean or, String field, String logic, String value, List<Rule> children) {
    this.or = or;
    this.field = field;
    this.logic = logic;
    this.value = value;
    this.children = children;
  }
}
